#include "enviroment/application.h"

uBoolean Application::initialized = false;
uBoolean Application::enableEditor = false;
UWindowBase *Application::window = nullptr;
uString Application::workPath = "";

int32 Application::Initialize(int argc, char **argv, Application *app)
{
    int init_count = 0;
    Application::workPath = argv[0];
    Application::initialized = true;
    if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
    {
        init_count++;
        Application::initialized = false;
        LOG_ERROR("SDL Init Failure: %s", SDL_GetError());
        // std::cout << "SDL Init Failure:" << SDL_GetError() << std::endl;
    }
    if (IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG | IMG_INIT_TIF) == 0)
    {
        init_count++;
        Application::initialized = false;
        LOG_ERROR("IMG Init Failure: %s", IMG_GetError());
        // std::cout << "IMG Init Failure:" << IMG_GetError() << std::endl;
    }
    if (TTF_Init() == -1)
    {
        init_count++;
        Application::initialized = false;
        LOG_ERROR("TTF Init Failure: %s", TTF_GetError());
        // std::cout << "TTF Init Failure:" << TTF_GetError() << std::endl;
    }
    if (Mix_Init(MIX_INIT_FLAC | MIX_INIT_MID | MIX_INIT_MP3 | MIX_INIT_OGG) == 0)
    {
        init_count++;
        Application::initialized = false;
        LOG_ERROR("Mix Init Failure: %s", Mix_GetError());
        // std::cout << "Mix Init Failure:" << Mix_GetError() << std::endl;
    }

    return init_count;
}

static uString GetPath(const uString &path)
{
    if (Application::workPath == "")
    {
        return "";
    }
    else
    {
        return Application::workPath + path;
    }
}

void Application::CreateWindow(const uString &caption, int32 width, int32 height, EWindowMode mode)
{
    if (!Application::initialized)
    {
        //
        LOG_ERROR("Application neccessary envirment not initialize.");
        // std::cout << "Application neccessary envirment not initialize." << std::endl;
        return;
    }
    if (Application::window)
    {
        LOG_WARN("Window Already Created.");
        return;
    }
    //=========================

    if (Application::enableEditor)
    {
        uString engineCaption = UTEXT(ENGINE_NAME_STRING);
        Application::window = new UEditorWindow(engineCaption, 1366, 768, EWindowMode::DESKTOP);
    }
    else
    {
        Application::window = new UGameWindow(caption, width, height, mode);
    }

    if (!Application::window)
    {
        LOG_ERROR("Create Window Failure: Bad malloc.");
        // std::cout << "Create Window Failure: Bad malloc." << std::endl;
        return;
    }
    if (window->__create() == false)
    {
        LOG_ERROR("Create Window Failure: Please Trace Last Error.");
        // std::cout << "Create Window Failure: Please Trace Last Error." << std::endl;
        return;
    }
}

Application::Application()
{
}

Application::~Application()
{
    if(GInputer){
        delete GInputer;
    }

    delete Application::window;

    Mix_Quit();
    TTF_Quit();
    IMG_Quit();
    SDL_Quit();
}

uBoolean Application::isInitialized() const
{
    return Application::initialized;
}

uString Application::getApplicationPath() const
{
    return Application::workPath;
}

void Application::onCreated()
{
}

void Application::onInput()
{
}

void Application::onDestroy()
{
}